package com.crm.dao;

import com.crm.dao.base.BaseHibernateDAO;
import com.crm.po.Menu;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Transaction;

public class MenuDAO extends BaseHibernateDAO {

	private static Logger logger = Logger.getLogger(MenuDAO.class);
	
	public void save(Menu menu) {
		if(menu.getId() == null || menu.getId().trim().length() == 0){
			menu.setId(this.getUUID());
		}
		Transaction transaction = getSession().beginTransaction();
		try {						
			this.getSession().save(menu);
			transaction.commit();
		} catch (RuntimeException e) {
			transaction.rollback();
			logger.error(e);
		} finally {
			this.closeSession();	
		}
	}

	public void saveOrUpdate(Menu menu) {
		if(menu.getId() == null || menu.getId().trim().length() == 0){
			menu.setId(this.getUUID());
		}
		Transaction transaction = getSession().beginTransaction();
		try {						
			this.getSession().saveOrUpdate(menu);
			transaction.commit();
		} catch (RuntimeException e) {
			transaction.rollback();
			logger.error(e);
		} finally {
			this.closeSession();	
		}
	}
	public void delete(Menu menu) {
		Transaction transaction = getSession().beginTransaction();
		try {
			this.getSession().delete(menu);
			transaction.commit();
		} catch (RuntimeException e) {
			transaction.rollback();
			logger.error(e);
		} finally {
			this.closeSession();	
		}
	}

	public Menu findById(String id) {
		Menu menu = null;
		try {
			
			menu = (Menu) this.getSession().get(Menu.class, id);
		} catch (RuntimeException e) {
			logger.error(e);
		} finally {
			this.closeSession();	
		}
		return menu;
	}
	
	@SuppressWarnings("unchecked")
	public List<Menu> menuList(String hql) {
		return (List<Menu>) findList(hql);
	}
}